//
//  BookcaseOprationController.m
//  ebook
//
//  Created by 吴超飞 on 16/9/30.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "BookcaseOprationController.h"

@interface BookcaseOprationController ()

@end

@implementation BookcaseOprationController

//加入书架
+(void)addBookcase:(NSString *) bookid success:(RequestSuccessed)successBlock failed:(RequestFailed)failedBlock{
    
    [EbookNetRequest addBookcase:bookid success:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
        successBlock(stateCode,successMsg,dict);
    } failed:^(int stateCode, NSString *failMsg) {
        if (stateCode == 1){
            [GuestLoginManager autoLogin:^(BOOL issuc) {
                if(issuc){
                    [BookcaseOprationController addBookcase:bookid success:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
                        successBlock(stateCode,successMsg,dict);
                    } failed:^(int stateCode, NSString *failMsg) {
                        failedBlock(stateCode,failMsg);
                    }];
                }
            }];
        }else{
            failedBlock(stateCode,failMsg);
        }
    }];
}

//获取书架
+(void)bookcaseSuccess:(RequestSuccessed)successBlock failed:(RequestFailed)failedBlock{
    
    NSTimeInterval loginInterval = [[NSDate date]timeIntervalSince1970] - [[NSUserDefaults standardUserDefaults] floatForKey:kLoginTime];
    
    if(loginInterval<6*60*60){//6小时之内有过登录，直接获取书架，如果服务端说未登录则登录后重试
        
        [EbookNetRequest bookcaseSuccess:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
            successBlock(stateCode,successMsg,dict);
        } failed:^(int stateCode, NSString *failMsg) {
            
            if (stateCode == 1){
                [GuestLoginManager autoLogin:^(BOOL issuc) {
                    if(issuc){
                        [EbookNetRequest bookcaseSuccess:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
                            successBlock(stateCode,successMsg,dict);
                        } failed:^(int stateCode, NSString *failMsg) {
                            failedBlock(stateCode,failMsg);
                        }];
                    }else{
                        failedBlock(stateCode,failMsg);
                    }
                }];
            }else{
                failedBlock(stateCode,failMsg);
            }
        }];
    }else{//没登录过则直接先登录后再获取书架
        [GuestLoginManager autoLogin:^(BOOL issuc) {
            if(issuc){
                [EbookNetRequest bookcaseSuccess:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
                    successBlock(stateCode,successMsg,dict);
                } failed:^(int stateCode, NSString *failMsg) {
                    failedBlock(stateCode,failMsg);
                }];
            }else{
                failedBlock(-1,@"登录失败");
            }
        }];
    }
}

+(void)loginAndGetBookcaseSuccess:(RequestSuccessed)successBlock failed:(RequestFailed)failedBlock{

}

//移除书架
+(void)removeBookcase:(NSString *) bookid success:(RequestSuccessed)successBlock failed:(RequestFailed)failedBlock{
    [EbookNetRequest removeBookcase:bookid success:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
        successBlock(stateCode,successMsg,dict);
    } failed:^(int stateCode, NSString *failMsg) {
        if (stateCode == 1){
            [GuestLoginManager autoLogin:^(BOOL issuc) {
                if(issuc){
                    [EbookNetRequest removeBookcase:bookid success:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
                        successBlock(stateCode,successMsg,dict);
                    } failed:^(int stateCode, NSString *failMsg) {
                        failedBlock(stateCode,failMsg);
                    }];
                }
            }];
        }else{
            failedBlock(stateCode,failMsg);
        }
    }];
}

//解锁
+(void)unlock:(RequestSuccessed)successBlock failed:(RequestFailed)failedBlock{
    [EbookNetRequest unlock:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
        successBlock(stateCode,successMsg,dict);
    } failed:^(int stateCode, NSString *failMsg) {
        if (stateCode == 1){
            [GuestLoginManager autoLogin:^(BOOL issuc) {
                if(issuc){
                    [EbookNetRequest unlock:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
                        successBlock(stateCode,successMsg,dict);
                    } failed:^(int stateCode, NSString *failMsg) {
                        failedBlock(stateCode,failMsg);
                    }];
                }
            }];
        }else{
            failedBlock(stateCode,failMsg);
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
